package com.os.web.controller;

import com.os.domain.DemoObj;
import org.springframework.lang.UsesSunHttpServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Matt
 * @Date 2022-01-16
 * @Desc:
 * @Version V1.0
 */
@Controller  //声明控制器
@RequestMapping("/anno")  //映射该控制器的访问路径为anno
public class DemoAnnoController {

    @RequestMapping(produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String index(HttpServletRequest request){
        return "url:" + request.getRequestURL() + " can access";
    }

    //演示路径参数，访问路径为/anno/pathvar/xxx
    @RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String demoPathVar(@PathVariable String str, HttpServletRequest request){
        return "url:" + request.getRequestURL() + " can access, str: " + str;
    }

    //演示常规request参数，访问路径为/anno/requestParam?id=1
    @RequestMapping(value="/requestParam", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String passRequestParam(Long id, HttpServletRequest request){
        return "url:" + request.getRequestURL() + " can acceaa, id: " + id;
    }

    //演示解释参数到对象，访问路径为/anno/obj?id=1&name=xxx
    @RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String passObj(DemoObj obj, HttpServletRequest request){
        return "url:" + request.getRequestURL() + " can access, obj id: " + obj.getId() + " obj name: " + obj.getName();
    }

    //演示不同的路径到相同的方法，访问路径为：/anno/name1或者/anno/name2
    @RequestMapping(value = {"/name1", "/name2"}, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String remove(HttpServletRequest request){
        return "url:" + request.getRequestURL() + " can access";
    }
}
